package com.googlecode.tracey.audit;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class TraceyAuditMessage<T extends TraceyAuditMessage> {
    private final Map<String, String> headers = new LinkedHashMap<String, String>();

    private Date logDate;

    private final String from;
    private final String to;
    private final Object payload;

    public TraceyAuditMessage(final String from, final String to, final Object payload) {
        this.from = from;
        this.to = to;
        this.payload = payload;
    }

    public T withDate(Date date) {
        this.logDate = date;
        return (T) this;
    }

    public T withHeader(String header, String value) {
        this.headers.put(header, value);
        return (T) this;
    }

    public Map<String, String> headers() {
        return headers;
    }

    public String from() {
        return from;
    }

    public String to() {
        return to;
    }

    public Object payload() {
        return payload;
    }

    public Date logDate() {
        return logDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof TraceyAuditMessage)) return false;

        TraceyAuditMessage that = (TraceyAuditMessage) o;

        if (from != null ? !from.equals(that.from) : that.from != null) return false;
        if (headers != null ? !headers.equals(that.headers) : that.headers != null) return false;
        if (logDate != null ? !logDate.equals(that.logDate) : that.logDate != null) return false;
        if (payload != null ? !payload.equals(that.payload) : that.payload != null) return false;
        if (to != null ? !to.equals(that.to) : that.to != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = headers != null ? headers.hashCode() : 0;
        result = 31 * result + (logDate != null ? logDate.hashCode() : 0);
        result = 31 * result + (from != null ? from.hashCode() : 0);
        result = 31 * result + (to != null ? to.hashCode() : 0);
        result = 31 * result + (payload != null ? payload.hashCode() : 0);
        return result;
    }
}
